package com.jy.adscm.operation.hscrb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.base.bean.JyGoods;
import com.jy.adscm.operation.base.mapper.mysql.JyGoodsMapper;
import com.jy.adscm.operation.hscrb.bean.YlMaterial;
import com.jy.adscm.operation.hscrb.bean.YlMaterial;
import com.jy.adscm.operation.hscrb.mapper.mysql.YlMaterialMapper;
import com.jy.adscm.operation.hscrb.service.YlMaterialService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-18
 */
@Service
public class YlMaterialServiceImpl extends ServiceImpl<YlMaterialMapper, YlMaterial> implements YlMaterialService {
    @Autowired
    JyGoodsMapper jyGoodsMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public Boolean updateYlMaterial(YlMaterial ylMaterial, Token token) {
        //待办修改
        if (ylMaterial.getTaskstate()==0 && ylMaterial.getTaskfinishstate()==0 &&
                "0".equals(ylMaterial.getIstaskstart()) &&  "0".equals(ylMaterial.getIstaskbegin()) &&
                "0".equals(ylMaterial.getIstaskend())  && "0".equals(ylMaterial.getIsreviewtask())
        ){


            return  super.updateById(ylMaterial);
        }
        //接收任务
        if (ylMaterial.getTaskstate()==0 && ylMaterial.getTaskfinishstate()==0 &&
                "1".equals(ylMaterial.getIstaskstart()) &&  "0".equals(ylMaterial.getIstaskbegin()) &&
                "0".equals(ylMaterial.getIstaskend())  && "0".equals(ylMaterial.getIsreviewtask())
        ){
            ylMaterial.setIstaskstart("1");
            ylMaterial.setTaskstartaddtime(new Date());
            ylMaterial.setTaskstate(1);

            return  super.updateById(ylMaterial);
        }
        //开始任务
        if (ylMaterial.getTaskstate()==1 && ylMaterial.getTaskfinishstate()==0 &&
                "1".equals(ylMaterial.getIstaskstart()) &&  "1".equals(ylMaterial.getIstaskbegin()) &&
                "0".equals(ylMaterial.getIstaskend())  && "0".equals(ylMaterial.getIsreviewtask())
        ){
            ylMaterial.setIstaskbegin("1");
            ylMaterial.setTaskbeginaddtime(new Date());
            return super.updateById(ylMaterial);
        }
        //结束任务
        if (ylMaterial.getTaskstate()==1 && ylMaterial.getTaskfinishstate()==0 &&
                "1".equals(ylMaterial.getIstaskstart()) &&  "1".equals(ylMaterial.getIstaskbegin()) &&
                "1".equals(ylMaterial.getIstaskend())  && "0".equals(ylMaterial.getIsreviewtask())
        ){
            ylMaterial.setIstaskend("1");
            ylMaterial.setTaskfinishstate(1);
            ylMaterial.setTaskendaddtime(new Date());
            return super.updateById(ylMaterial);
        }
        //复核任务
        if (ylMaterial.getTaskstate()==1 && ylMaterial.getTaskfinishstate()==1 &&
                "1".equals(ylMaterial.getIstaskstart()) &&  "1".equals(ylMaterial.getIstaskbegin()) &&
                "1".equals(ylMaterial.getIstaskend())  && "1".equals(ylMaterial.getIsreviewtask())
        ){
            ylMaterial.setIsreviewtask("1");
            ylMaterial.setReviewaddtime(new Date());
            ylMaterial.setReviewperson(token.getUser().getId().toString());
            return super.updateById(ylMaterial);
        }

        return null;
    }

    @Override
    public List<YlMaterial> selectYlMaterial(Integer taskState, Integer taskFinishState, Integer isReviewTask) {
        Wrapper<YlMaterial> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",taskState);
        wrapper.eq("TASKFINISHSTATE",taskFinishState);
        wrapper.eq("isReviewTask",isReviewTask);
        List<YlMaterial> ylMaterials= super.selectList(wrapper);



        for (YlMaterial ylMaterial : ylMaterials) {
            if ( ylMaterial.getTaskreleaseperson()!=null && !"".equals( ylMaterial.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(ylMaterial.getTaskreleaseperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    ylMaterial.setTaskreleasepersonName(jyUser.getName());
                }else {
                    ylMaterial.setTaskreleasepersonName(ylMaterial.getTaskreleaseperson());
                }

            }
            if ( ylMaterial.getTaskperformperson()!=null && !"".equals( ylMaterial.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(ylMaterial.getTaskperformperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    ylMaterial.setTaskperformpersonName(jyUser.getName());
                }else {
                    ylMaterial.setTaskperformpersonName(ylMaterial.getTaskperformperson());
                }

            }

            if ( ylMaterial.getReviewperson()!=null && !"".equals( ylMaterial.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(ylMaterial.getReviewperson());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    ylMaterial.setReviewpersonName(jyUser.getName());
                }else {
                    ylMaterial.setReviewpersonName(ylMaterial.getReviewperson());
                }

            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(ylMaterial.getResourcename(),null)){
                JyGoods jyGoods = jyGoodsMapper.selectById(ylMaterial.getResourcename());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyGoods,null)){
                    ylMaterial.setResourcenameName(jyGoods.getName());
                }else {
                    ylMaterial.setResourcenameName(ylMaterial.getResourcename());
                }


            }

        }

        return ylMaterials;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("114");
        return jyUsers;
    }

    @Override
    public List<JyGoods> queryMaterial() {
        Wrapper<JyGoods> wrapper =new EntityWrapper<>();
        wrapper.setSqlSelect("TID","NAME");
        wrapper.eq("PRODUCTUSE","0");
        List<JyGoods> jyGoods = jyGoodsMapper.selectList(wrapper);
        return jyGoods;
    }
}
